{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "311075a5",
   "metadata": {},
   "source": [
    "# 字典（dict）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3dda9e9",
   "metadata": {},
   "source": [
    "### dict.clear()\n",
    "- 清空字典中所有的 key-value，该字典变成一个空字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e5a23aec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-27T01:21:45.498134Z",
     "start_time": "2021-10-27T01:21:45.483116Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu01': 'AA', 'stu02': 'BB', 'stu03': 'CC'}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"AA\",\n",
    "    \"stu02\":\"BB\",\n",
    "    \"stu03\":\"CC\"\n",
    "}\n",
    "print(stu_info)\n",
    "stu_info.clear()\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e45774d2",
   "metadata": {},
   "source": [
    "### dict.get()\n",
    "- 根据 key 来获取 value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d2171cd6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-27T01:28:47.713419Z",
     "start_time": "2021-10-27T01:28:47.706417Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AA\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"AA\",\n",
    "    \"stu02\":\"BB\",\n",
    "    \"stu03\":\"CC\"\n",
    "}\n",
    "# 获取 stu01 的学号\n",
    "print(stu_info.get('stu01'))\n",
    "\n",
    "# 但如果使用 get() 方法访问不存在的 key，该方法会简单地返回 None，不会导致错误\n",
    "print(stu_info.get('stu05'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f86fe75",
   "metadata": {},
   "source": [
    "### dict.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6fa4d8ba",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-27T01:32:42.227147Z",
     "start_time": "2021-10-27T01:32:42.209212Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu01': 'AA', 'stu02': 'BB', 'stu03': 'CC'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"AA\",\n",
    "    \"stu02\":\"BB\",\n",
    "    \"stu03\":\"CC\"\n",
    "}\n",
    "stu_info.copy()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9486d30c",
   "metadata": {},
   "source": [
    "### dict.fromkeys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c7d5c201",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-27T01:40:36.901171Z",
     "start_time": "2021-10-27T01:40:36.880167Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': None, 'b': None}\n",
      "{13: 'god', 17: 'god'}\n"
     ]
    }
   ],
   "source": [
    "# 用给定的多个 key 创建字典，这些 key 对应的 value 默认都是 None\n",
    "\n",
    "dict = dict.fromkeys(['a', 'b'])\n",
    "print(dict)\n",
    "\n",
    "# 可以额外加入一个参数作为默认的 value\n",
    "dict02 = dict.fromkeys((13, 17), 'god')\n",
    "print(dict02)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad2816bd",
   "metadata": {},
   "source": [
    "### dict.items()\n",
    "- 以列表返回可遍历的（键，值）元组数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cebdd557",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-28T13:08:35.277917Z",
     "start_time": "2021-10-28T13:08:35.258913Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('stu01', 'AA'), ('stu02', 'BB'), ('stu03', 'CC')])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"AA\",\n",
    "    \"stu02\":\"BB\",\n",
    "    \"stu03\":\"CC\"\n",
    "}\n",
    "stu_info.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "381fe3a9",
   "metadata": {},
   "source": [
    "### dict.keys()\n",
    "- 以列表返回一个字典所有的键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "51d5c347",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-28T13:10:34.581067Z",
     "start_time": "2021-10-28T13:10:34.564065Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['stu01', 'stu02', 'stu03'])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"AA\",\n",
    "    \"stu02\":\"BB\",\n",
    "    \"stu03\":\"CC\"\n",
    "}\n",
    "stu_info.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3514b321",
   "metadata": {},
   "source": [
    "### dict.pop()\n",
    "- 删除字典给定键key所对应的值    key值必须给出,否则，返回default值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "46cb310e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-28T13:19:25.996302Z",
     "start_time": "2021-10-28T13:19:25.984298Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BB\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"AA\",\n",
    "    \"stu02\":\"BB\",\n",
    "    \"stu03\":\"CC\"\n",
    "}\n",
    "a = stu_info.pop('stu02')\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "139c37f6",
   "metadata": {},
   "source": [
    "### dict.popitem()\n",
    "- 随机返回并删除字典中的一对键和值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "838fcf09",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-28T13:20:47.789757Z",
     "start_time": "2021-10-28T13:20:47.767752Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu03', 'CC')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"AA\",\n",
    "    \"stu02\":\"BB\",\n",
    "    \"stu03\":\"CC\"\n",
    "}\n",
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55158fd7",
   "metadata": {},
   "source": [
    "### dict.setdefault()   \n",
    "- 和dict.get()类似\n",
    "- 如果dict中有key，则返回key值，如果没有找到key，则在dict中加上该key，值由default参数给出，默认None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "599b24f0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-28T13:25:26.176334Z",
     "start_time": "2021-10-28T13:25:26.162331Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AA\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu01\":\"AA\",\n",
    "    \"stu02\":\"BB\",\n",
    "    \"stu03\":\"CC\"\n",
    "}\n",
    "print(stu_info.setdefault('stu01'))\n",
    "print(stu_info.setdefault('stu05'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58dca994",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
